//
//  ZWKHeaderView.m
//  ZWKPicLoop
//
//  Created by MS on 15/11/19.
//
//

#import "ZWKHeaderView.h"
#import "Masonry.h"
#define HeaderViewSize self.bounds.size
@interface ZWKHeaderView ()<UIScrollViewDelegate>
@property (nonatomic,weak) UIScrollView * scrollView;
@property (nonatomic,weak) UIPageControl * pageControl;
@property (nonatomic,weak) UIImageView * leftImageView;
@property (nonatomic,weak) UIImageView * centerImageView;
@property (nonatomic,weak) UIImageView * rightImageView;
@property (nonatomic,assign) NSInteger currentIndex;
@property (nonatomic,strong) NSTimer * timer;
@end
@implementation ZWKHeaderView

+ (id)headView{

    return [[self alloc]init];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self.timer invalidate];
        self.timer = nil;
    }
    return self;
}
- (void)setImageNames:(NSArray *)imageNames{
    
    _imageNames = imageNames;
    [self configImageViews];
    [self configScrollView];
    [self configPageControl];
    [self configTimer];
}
#pragma mark 对UIScrollView ,PageControl 等的数据初始化
- (void) configTimer{
 
    self.timer = [[NSTimer alloc]initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:self.fireSeconds == 0.0f ? 2.0f:self.fireSeconds] interval:self.timerInterval == 0 ? 2.0f : self.timerInterval target:self selector:@selector(dealTimer) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    
}
- (void)dealTimer{
    
    self.pageControl.currentPage = (self.pageControl.currentPage + 1)%self.pageControl.numberOfPages;
    self.currentIndex = (self.currentIndex + 1)%self.pageControl.numberOfPages;
    [self reloadImages];
}
#pragma mark   some config works
- (void)configScrollView{
    self.scrollView.delegate = self;
    self.scrollView.contentOffset = CGPointMake(HeaderViewSize.width, 0);
}
- (void)configPageControl{
    
    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.scrollView.mas_bottom).offset(-10);
        make.centerX.mas_equalTo(self.scrollView.mas_centerX);
    }];
}
- (void)configImageViews{
    self.leftImageView.image = [UIImage imageNamed:self.imageNames.lastObject];
    self.centerImageView.image = [UIImage imageNamed:self.imageNames.firstObject];
    self.rightImageView.image = [UIImage imageNamed:self.imageNames[1]];
}
#pragma mark UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self updateCurrentIndex];
    [self reloadImages];
    scrollView.contentOffset = CGPointMake(HeaderViewSize.width, 0);
    self.pageControl.currentPage = self.currentIndex;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.timer setFireDate:[NSDate distantFuture]];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self.timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:self.fireSeconds == 0.0f ? 2.0f:self.fireSeconds]];
}
- (void)updateCurrentIndex{
    CGPoint contentOffset = self.scrollView.contentOffset;
    if(contentOffset.x < HeaderViewSize.width){
        self.currentIndex = (self.currentIndex + self.imageNames.count - 1) % self.imageNames.count;
    }else if(contentOffset.x > HeaderViewSize.width){
        self.currentIndex = (self.currentIndex + 1)%self.imageNames.count;
    }
}
- (void)reloadImages{
    
    self.leftImageView.image = [UIImage imageNamed:self.imageNames[(self.currentIndex + self.imageNames.count - 1)%self.imageNames.count]];
    self.centerImageView.image = [UIImage imageNamed:self.imageNames[self.currentIndex]];
    self.rightImageView.image = [UIImage imageNamed:self.imageNames[(self.currentIndex + 1)%self.imageNames.count]];
}
#pragma mark 懒加载
- (UIScrollView *)scrollView{

    if(_scrollView == nil){
        UIScrollView * scrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
        scrollView.bounces = NO;
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.pagingEnabled = YES;
        scrollView.contentSize = CGSizeMake(HeaderViewSize.width * 3, HeaderViewSize.height);
        [self addSubview:scrollView];
        self.scrollView = scrollView;
    }
    return _scrollView;
}
- (UIPageControl *)pageControl{
    if (_pageControl == nil) {
        UIPageControl * pageControl = [[UIPageControl alloc]init];
        pageControl.numberOfPages = self.imageNames.count;
        pageControl.currentPageIndicatorTintColor = self.currentPageIndicatorTintColor == nil ? [UIColor redColor] : self.currentPageIndicatorTintColor;
        pageControl.pageIndicatorTintColor = self.pageIndicatorTintColor == nil ? [UIColor blueColor] : self.pageIndicatorTintColor;
        [self addSubview:pageControl];
        self.pageControl = pageControl;
    }
    return _pageControl;
}
- (UIImageView *)leftImageView{
    if(_leftImageView == nil){
        UIImageView * leftImageView = [[UIImageView alloc]init];
        [self.scrollView addSubview:leftImageView];
        
        [leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.scrollView.mas_left);
            make.top.equalTo(self.scrollView.mas_top);
            make.height.equalTo(self.scrollView.mas_height);
            make.width.equalTo(self.scrollView.mas_width);
        }];
        
        self.leftImageView = leftImageView;
        
    }
   
    return _leftImageView;
}
- (UIImageView *)centerImageView{
    if(_centerImageView == nil){
        UIImageView * centerImageView = [[UIImageView alloc]init];
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dealHeaderSelected)];
        centerImageView.userInteractionEnabled = YES;
        [centerImageView addGestureRecognizer:tap];
        [self.scrollView addSubview:centerImageView];
        
        [centerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.left.equalTo(self.leftImageView.mas_right);
            make.top.equalTo(self.leftImageView.mas_top);
            make.width.equalTo(self.leftImageView.mas_width);
            make.height.equalTo(self.leftImageView
                                .mas_height);
        }];
        
        
        self.centerImageView = centerImageView;
    }
    return _centerImageView;
}
- (UIImageView *)rightImageView{
    if(_rightImageView == nil){
        UIImageView * rightImageView = [[UIImageView alloc]init];
        [self.scrollView addSubview:rightImageView];
        
        [rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.left.equalTo(self.centerImageView.mas_right);
            make.top.equalTo(self.centerImageView.mas_top);
            make.height.equalTo(self.centerImageView.mas_height);
            make.width.equalTo(self.centerImageView.mas_width);
        }];
       
        self.rightImageView = rightImageView;
    }
    return _rightImageView;
}
- (void)dealHeaderSelected{

    if (self.delegate && [self.delegate respondsToSelector:@selector(headerViewDidSelected:atIndex:)]) {
        [self.delegate headerViewDidSelected:self atIndex:self.currentIndex];
    }
}
@end
